package com.zcdyy.rentalofhouse.ui.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zcdyy.rentalofhouse.R;
import com.zcdyy.rentalofhouse.application.MyApplication;
import com.zcdyy.rentalofhouse.bean.Account;
import com.zcdyy.rentalofhouse.bean.OrderInfo;
import com.zcdyy.rentalofhouse.bean.SaleHouse;
import com.zcdyy.rentalofhouse.constant.Constants;
import com.zcdyy.rentalofhouse.utils.BaseTranslucentActivity;
import com.zcdyy.rentalofhouse.utils.BmobUtils;
import com.zcdyy.rentalofhouse.utils.ImageLoaderUtils;
import com.zcdyy.rentalofhouse.utils.ImageUploadUtil;
import com.zcdyy.rentalofhouse.utils.ImageUtil;
import com.zcdyy.rentalofhouse.utils.ToastUtils;
import com.zcdyy.rentalofhouse.views.ActionSheet;
import com.zcdyy.rentalofhouse.views.CircleImageView;

import java.io.File;

import cn.bmob.v3.datatype.BmobFile;

public class SaleActivity extends BaseTranslucentActivity implements View.OnClickListener, ActionSheet.OnSheetItemClickListener {

    private static int type = 1;//0表示整租，1表示二手房，2表示单间，3表示商铺
    //拍照、从相册选择、照片保存成功
    private final int TAKE_PHOTO = 1, CHOOSE_PHOTO = 2, SAVE_IMAGE_SUCCESS = 3;
    public SaleHouse saleHouse;//表示二手房
    public OrderInfo orderInfo;
    private TextView tvPublish;//发布按钮
    private TextView tvTitle;
    private String[] items = {"拍照", "我的相册"};
    // 头像文件、上传头像的名称、本地图片uri
    private File imageFile;
    private Uri imageUri; // 图片路径
    private String saveFileName = "";
    private BmobUtils bmobUtils;
    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Constants.ResultCode.SUCCESS:
                    ImageLoaderUtils.initImage(SaleActivity.this,saleHouse.getPic().getFileUrl(),
                            civ_head,R.drawable.chat_xe_icon2_03);
                    dialog.dismiss();
                    break;
                case Constants.ResultCode.UPLOAD_SUCCESS:
                    dialog.dismiss();
                    ToastUtils.shortToast(SaleActivity.this, "发布成功");
                    BmobUtils.query = false;
                    BmobUtils.queryCount = 0;
                    startActivity(new Intent(SaleActivity.this, MainActivity.class));
                    finish();
                    break;
            }
        }
    };
    private CircleImageView civ_head;//用户上传的房屋图片
    private EditText etXiaoQu;
    private EditText etQuYu;
    private EditText etHuXingShi;
    private EditText etHuXingTing;
    private EditText etHuXingWei;
    private EditText etSaleLouCeng;
    private EditText etSaleAllLouCeng;
    private EditText etMianJi;
    private EditText etShouJia;
    private RelativeLayout rlZhuangXiu;//装修点击界面
    private EditText etBiaoTi;
    private RelativeLayout rlDesc;//描述点击界面
    private TextView tvZhuangXiu;
    private TextView tvDescChoose;
    private EditText etLianXiRen;
    private EditText etShouJiHao;
    private Account loginInfo;//当前用户

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loginInfo = MyApplication.getInstance().readLoginUser();//获取当前用户
        setContentView(R.layout.activity_sale);

        initView();
        initTitle();
        initData();
        bind();
    }

    private void initData() {
        saleHouse = new SaleHouse();
        orderInfo = new OrderInfo();
        bmobUtils = new BmobUtils(this);
        Intent intent = getIntent();
        if (intent != null) {
            String str = intent.getStringExtra("type");
            if (str != null) {
                tvTitle.setText(str);
            }
        }
    }

    private void bind() {
        civ_head.setOnClickListener(this);
        rlZhuangXiu.setOnClickListener(this);
        tvDescChoose.setOnClickListener(this);
        rlDesc.setOnClickListener(this);
        tvPublish.setOnClickListener(this);
    }

    private void initView() {
        tvTitle = (TextView) findViewById(R.id.top_tv_title);
        civ_head = (CircleImageView) findViewById(R.id.civ_header);
        etXiaoQu = (EditText) findViewById(R.id.et_xiaoqu1);
        etQuYu = (EditText) findViewById(R.id.et_quyu);
        etHuXingShi = (EditText) findViewById(R.id.et_huxing_shi);
        etHuXingTing = (EditText) findViewById(R.id.et_huxing_ting);
        etHuXingWei = (EditText) findViewById(R.id.et_huxing_wei);
        etSaleLouCeng = (EditText) findViewById(R.id.et_sale_louceng);
        etSaleAllLouCeng = (EditText) findViewById(R.id.et_sale_alllouceng);
        etMianJi = (EditText) findViewById(R.id.et_mianji);
        etShouJia = (EditText) findViewById(R.id.et_shoujia);
        rlZhuangXiu = (RelativeLayout) findViewById(R.id.rl_zhuangxiu);
        etBiaoTi = (EditText) findViewById(R.id.et_sale_biaoti);
        rlDesc = (RelativeLayout) findViewById(R.id.rl_desc);
        tvZhuangXiu = (TextView) findViewById(R.id.tv_zhuangxiu);
        tvDescChoose = (TextView) findViewById(R.id.tv_miaoshu_choose);
        etLianXiRen = (EditText) findViewById(R.id.et_lianxiren);
        etShouJiHao = (EditText) findViewById(R.id.et_shoujihao);
        tvPublish = (TextView) findViewById(R.id.bt_publish);
    }

    private void initTitle() {
        tvTitle.setText("二手房");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.civ_header:
                showActionSheetDialog(items);
                break;
            case R.id.rl_zhuangxiu:
                Intent intent1 = new Intent(SaleActivity.this,PayStyleActivity.class);
                intent1.putExtra("type","sale");
                startActivityForResult(intent1,600);
                break;
            case R.id.rl_desc:
            case R.id.tv_miaoshu:
                Intent intent = new Intent(SaleActivity.this, MoreDescActivity.class);
                startActivityForResult(intent, 200);
                break;
            case R.id.bt_publish:
                publishRental();
                break;
        }
    }

    private void showActionSheetDialog(String[] items) {
        ActionSheet actionSheet = new ActionSheet(this)
                .builder()
                .setCancelable(false)
                .setCanceledOnTouchOutside(true);
        for (int i = 0; i < items.length; i++) {
            actionSheet.addSheetItem(items[i], ActionSheet.SheetItemColor.Blue, SaleActivity.this);
        }
        actionSheet.show();
    }

    @Override
    public void onClick(int which) {
        switch (which) {
            case TAKE_PHOTO:
                //拍照
                imageFile = ImageUploadUtil.createImageFile(ImageUploadUtil.CAMERA_SAVEDIR2, ImageUploadUtil.createImageName());
                startActivityForResult(ImageUploadUtil.intentImageCapture(imageFile), ImageUploadUtil.TAKE_PHOTO);
                break;
            case CHOOSE_PHOTO:
                //从相册选择
                startActivityForResult(ImageUtil.intentChooseImg(),
                        CHOOSE_PHOTO);
                break;
        }
    }

    /**
     * 1\发布二手房租房信息
     */
    private void publishRental() {
        if (check()) {

            saleHouse.setId(loginInfo.getId());
            saleHouse.setXiaoQu(etXiaoQu.getText().toString());
            saleHouse.setQuYu(etQuYu.getText().toString());
            saleHouse.setShi(etHuXingShi.getText().toString());
            saleHouse.setTing(etHuXingTing.getText().toString());
            saleHouse.setWei(etHuXingWei.getText().toString());
            saleHouse.setLouCeng(etSaleLouCeng.getText().toString());
            saleHouse.setZongLouCeng(etSaleAllLouCeng.getText().toString());
            saleHouse.setMiaoJi(etMianJi.getText().toString());
            saleHouse.setShouJia(etShouJia.getText().toString());
            saleHouse.setZhuangXiu(tvZhuangXiu.getText().toString());
            saleHouse.setBiaoTi(etBiaoTi.getText().toString());
            saleHouse.setMiaoShu(tvDescChoose.getText().toString());
            saleHouse.setLianXiRen(etLianXiRen.getText().toString());
            saleHouse.setShouJiHao(etShouJiHao.getText().toString());

            orderInfo.setId(loginInfo.getId());
            orderInfo.setTitle(etBiaoTi.getText().toString());
            orderInfo.setCity(etQuYu.getText().toString());
            orderInfo.setRent_money(etShouJia.getText().toString());
            orderInfo.setType("1");

//            BmobFile bmobFile = new BmobFile(imageFile);
//            bmobUtils.upHouseFile(bmobFile);//这里上传二手房图片
            dialog = ProgressDialog.show(this, null, "正在发布 ...");
            bmobUtils.upHouseRental(saleHouse, orderInfo);
        } else {
            ToastUtils.shortToast(this, "请填写完整信息哦~~");
        }
    }

    private boolean check() {
        if (imageFile == null || etXiaoQu.getText().toString().equals("")
                || etQuYu.getText().toString().equals("") || etHuXingShi.getText().toString().equals("")
                || etHuXingTing.getText().toString().equals("") || etHuXingWei.getText().toString().equals("")
                || etSaleLouCeng.getText().toString().equals("") || etSaleAllLouCeng.getText().toString().equals("")
                || etMianJi.getText().toString().equals("") || etShouJia.getText().toString().equals("")
                || etBiaoTi.getText().toString().equals("") || tvDescChoose.getText().toString().equals("")
                || etLianXiRen.getText().toString().equals("") || etShouJiHao.getText().toString().equals("")) {
            return false;
        } else {
            return true;
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //Log.e("steq",requestCode+"--"+data.getStringExtra("payStyle")+"");
        switch (requestCode) {
            case 600:
                if (data != null) {
                    String strl = data.getStringExtra("payStyle");
                    Log.e("steq",strl);
                    tvZhuangXiu.setText(strl);
                }
                break;
            case 200:
                if (data == null) {
                    tvDescChoose.setHint("房源描述");
                } else {
                    String str = data.getStringExtra("desc");
                    tvDescChoose.setText(str);
                }
                break;

            case TAKE_PHOTO:
                if (resultCode == 0) {
                    return;
                }
                //拍照
                ImageUtil.paizhaocreateImagefile(this, imageFile);
                if (imageFile == null) {
                    ToastUtils.shortToast(this, "图片太大无法上传");
                    return;
                }
                doSendThread();//上传图片
                break;
            case CHOOSE_PHOTO:
                //相册
                if (resultCode == 0) return;
                imageUri = data.getData();
                imageFile = MyApplication.getInstance().createimagefile(imageUri, getVmWidth(), getVmHeight());
                if (imageFile == null) {
                    ToastUtils.shortToast(this, "imageFile为空2");
                    return;
                }
                doSendThread();//上传图片
                break;
        }
    }

    /**
     * 上传图片
     */
    private void doSendThread() {
        Log.e("image", imageFile.getPath() + "");
        dialog = ProgressDialog.show(this, null, "正在上传照片 ...");
        BmobFile bmobFile = new BmobFile(imageFile);
        bmobUtils.upHouseFile(bmobFile);//这里上传二手房图片

//        Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getPath());
//        civ_head.setImageBitmap(bitmap);
    }
}
